const urlParams = new URLSearchParams(window.location.search);
const loginName = urlParams.get('loginName');
const phoneStr=/^(13[0-9]|15[0-35-9]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
const emailStr=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/
const idStr=/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
new Vue({
    el:"#updaeUser",
    data:{
        infoFlag:false,
        infoText:'',
        user:'',
        oldLoginName:''
    },
    methods:{
        getUserByLoginName()
        {
          axios.get('/api/checkLoginName',{
              params:{
                  loginName
              }
          })  .then(rs=>{
              this.user=rs.data.user
              this.oldLoginName=rs.data.user.loginName
          })
        },
        updateSave()
        {
            if(this.user.userName==='')
            {
                this.infoFlag=true
                this.infoText="请填写用户名"
                return
            }
            if(this.user.loginName==='')
            {
                this.infoFlag=true
                this.infoText="请填写登录名"
                return
            }
            if(this.user.email==='')
            {
                this.infoFlag=true
                this.infoText="请填写邮箱"
                return
            }
            if(!emailStr.test(this.user.email))
            {
                this.infoFlag=true
                this.infoText="邮箱格式不正确（xx@ss.ss）"
                return
            }
            if(this.user.mobile==='')
            {
                this.infoFlag=true
                this.infoText="请填写手机号"
                return
            }
            if(!phoneStr.test(this.user.mobile))
            {
                this.infoFlag=true
                this.infoText="手机号格式不正确（13 | 15 | 17 | 18 | 19开头）"
                return
            }
            if(this.user.identityCode==='')
            {
                this.infoFlag=true
                this.infoText="请填写身份证"
                return
            }
            if(!idStr.test(this.user.identityCode))
            {
                this.infoFlag=true
                this.infoText="身份证格式不正确"
                return
            }
            axios.post('/api/updateUser',
                    this.user
            ).then(rs=>{
                this.infoFlag=true
                if(rs.data.isUpdate==="yes")
                {
                    this.infoText="修改成功"
                    setTimeout(()=>{
                        window.location="/api/userManage.html"
                    },1000)
                }else{
                    this.infoText="修改失败"
                }
            })
        },
        //重置密码
        resetPassword()
        {
            const pass="123456"
            axios.get('/api/updateUserPass',{
                params:{
                    pass,
                    id:this.user.id
                }
            }).then(rs=>{
                this.infoFlag=true
                if(rs.data.isUpdate==="yes")
                {
                    this.infoText="重置成功"
                }else{
                    this.infoText="重置失败"
                }
            })
        },
        checkLoginName()
        {
            axios.get('/api/checkLoginName',{
                params:{
                    loginName:this.user.loginName
                }
            }).then(rs=>{
                if(rs.data.isRepeat==="yes")
                {
                    this.$refs.lg_name.innerHTML="用户名重复"
                    this.$refs.lg_name.style.color="red"
                    this.$refs.updateSave.disabled=true
                }
                if(rs.data.user.loginName===this.oldLoginName||rs.data.isRepeat==="no"){
                    this.$refs.lg_name.innerHTML="用户名可用"
                    this.$refs.lg_name.style.color="green"
                    this.$refs.updateSave.disabled=false
                }
            })
        },
        closeInfo()
        {
            this.infoFlag=false
        },

    },
    created()
    {
        this.getUserByLoginName()
    }
})